package com.stockz.equity.indicator.rateOfChange;

import java.util.List;

import org.springframework.stereotype.Component;

import com.stockz.equity.indicator.Indicator;
import com.stockz.equity.model.Bar;
import com.stockz.equity.model.Bar.BarValueType;

@Component
public class RateOfChangeIndicator implements Indicator<RateOfChangeIndicatorConfiguration> {

	private RateOfChangeIndicatorConfiguration rateOfChangeIndicatorConfiguration
	 = new RateOfChangeIndicatorConfiguration();
	private BarValueType barValueType = BarValueType.AVGPRICE;
	
	@Override
	public double[] getValues(List<Bar> barList) {
		int span = rateOfChangeIndicatorConfiguration.getSpan();
		double[] values = new double[barList.size()];
		for(int i = barList.size()-1; i >=0; i--){
			if(i >= barList.size() - span - 1){
				values[i] = 0;
			}else{
				values[i] = getValue(barList.subList(i, i+span+1));
			}
		}
		return values;
	}

	@Override
	public double getValue(List<Bar> barList) {
		int span = rateOfChangeIndicatorConfiguration.getSpan();
		BarValueType barValueType = getBarValueType();
		if(barList == null || barList.isEmpty() || barList.size() <= span){
			return 0;
		}
		double value = barList.get(span).getValue(barValueType);
		if(value == 0){
			return 0;
		}
		return (barList.get(0).getValue(barValueType) - value)*100/value;
	}

	@Override
	public RateOfChangeIndicatorConfiguration getIndicatorConfiguration() {
		return rateOfChangeIndicatorConfiguration;
	}

	public BarValueType getBarValueType() {
		return barValueType;
	}

	public void setBarValueType(BarValueType barValueType) {
		this.barValueType = barValueType;
	}
	
}
